package com.ruoyi.tmsdriveracc.controller;

import com.aliyun.oss.OSSClient;
import com.ruoyi.tmsdriveracc.domain.AError;
import com.ruoyi.tmsdriveracc.domain.AErrorImages;
import com.ruoyi.tmsdriveracc.domain.Waybill;
import com.ruoyi.tmsdriveracc.mapper.AErrorDao;
import com.ruoyi.tmsdriveracc.mapper.AErrorImagesDao;
import com.ruoyi.tmsdriveracc.service.AErrorService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;

/**
 * (AError)表控制层
 *
 * @author 小何
 * @since 2022-04-24 00:13:00
 */
@RestController
@RequestMapping("aError")
public class AErrorController {
    @Resource
    private OSSClient ossClient;

    private static final String BUCKETNAME = "xiaohe1996";
    /**
     * 服务对象
     */
    @Resource
    private AErrorService aErrorService;
    @Resource
    private AErrorDao aErrorDao;
    @Resource
    private AErrorImagesDao aErrorImagesDao;

    /**
     * 分页查询
     *
     * @param aError      筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping("/queryByPage")
    public ResponseEntity<Page<AError>> queryByPage(AError aError, PageRequest pageRequest) {
        return ResponseEntity.ok(this.aErrorService.queryByPage(aError, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param
     * @return 单条数据
     */
    @GetMapping("/queryById")
    public ResponseEntity<AError> queryById(Waybill waybill) {
        return ResponseEntity.ok(this.aErrorService.queryById(waybill));
    }

    /**
     * 新增数据
     *
     * @param aError 实体
     * @return 新增结果
     */
    @GetMapping("/add")
    public ResponseEntity<AError> add(AError aError) {
        return ResponseEntity.ok(this.aErrorService.insert(aError));
    }

    /**
     * 编辑数据
     *
     * @param aError 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<AError> edit(AError aError) {
        return ResponseEntity.ok(this.aErrorService.update(aError));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.aErrorService.deleteById(id));
    }

    @PostMapping("/uploadErrorImgs")
    public void uploadErrorImgs(MultipartHttpServletRequest request) {

        List<MultipartFile> files = request.getFiles("file");
        String contractId = request.getParameter("contractId");
        AError aError = aErrorDao.queryById(Integer.valueOf(contractId));
        AErrorImages aErrorImages = new AErrorImages();
        aErrorImages.setErrorId(aError.getId());
        for (MultipartFile file : files) {
            //1.获得文件名字
            String fileName = file.getOriginalFilename();
            //2.获取文件流
            InputStream inputStream = null;
            try {
                inputStream = file.getInputStream();
                ossClient.putObject(BUCKETNAME, fileName, inputStream);
                //设置文件有效期  60天
                Date date = new Date(System.currentTimeMillis() + 1000 * 3600 * 24 * 60);
                //获取url
                URL url = ossClient.generatePresignedUrl(BUCKETNAME, fileName, date);
                aErrorImages.setPath(url.toString());
                aErrorImagesDao.insert(aErrorImages);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //删除文件
    @PostMapping("/delErrorImgs")
    public void delErrorImgs(String fileName) {
        ossClient.deleteObject(BUCKETNAME, fileName);
    }
}

